/**
 * @file alarm_set.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include <stdio.h>
#include "lv_watch.h"

#if USE_LV_WATCH_ALARM_SET

/*********************
 *      DEFINES
 *********************/
#define  ALARM_TIME_STR_LENGTH       12

static const watchLangTxtId_t week_txt_id[] = {
    WATCH_TEXT_ID_CALENDAR_SUNDAY,
    WATCH_TEXT_ID_CALENDAR_MONDAY,
    WATCH_TEXT_ID_CALENDAR_TUESDAY,
    WATCH_TEXT_ID_CALENDAR_WEDNESDAY,
    WATCH_TEXT_ID_CALENDAR_THURSDAY,
    WATCH_TEXT_ID_CALENDAR_FRIDAY,
    WATCH_TEXT_ID_CALENDAR_SATURDAY,
};

/**********************
 *  STATIC VARIABLES
**********************/
static alarm_curr_t alarm_curr;

/**********************
 *  STATIC PROTOTYPES
 **********************/
//alarm list
static void alarm_list_prepare_destory(lv_obj_t * activity_obj);
static lv_alarm_list_obj_ext_t * alarm_list_get_ext(void);
static void alarm_list_get_repeat_str(uint8_t repeat_bitmap, char *str, uint8_t len);
static int8_t alarm_list_get_min_index(app_adaptor_alarm_t * alarm);
static lv_obj_t * alarm_list_add(lv_obj_t * list, app_adaptor_alarm_t * alarm, uint8_t index);
static lv_obj_t * alarm_list_add_exceed_max_create(lv_obj_t * par);
static void alarm_list_add_exceed_max_destroy(lv_anim_t * anim);
static void alarm_list_switch_action(lv_obj_t * obj, lv_event_t e);
static void alarm_list_add_btn_action(lv_obj_t * obj, lv_event_t e);
static void alarm_list_edit_btn_action(lv_obj_t * obj, lv_event_t e);

//set time
static lv_obj_t * alarm_set_time_roller_create(lv_obj_t * par, hal_rtc_t * date_time);
static void alarm_set_time_roller_action(lv_obj_t * roller, lv_event_t e);
static lv_obj_t * alarm_opt_create(lv_obj_t * par);
static void alarm_cancel_btn_action(lv_obj_t * obj, lv_event_t e);
static void alarm_ok_btn_action(lv_obj_t * obj, lv_event_t e);

//set repeat
static lv_obj_t * alarm_set_repeat_create(lv_obj_t * par);

//alarm add
static lv_obj_t * alarm_add_create(lv_obj_t * activity_obj);
static void alarm_add_prepare_destory(lv_obj_t * activity_obj);

//alarm edit
static lv_obj_t * alarm_edit_create(lv_obj_t * activity_obj);
static void alarm_edit_prepare_destory(lv_obj_t * activity_obj);
static void alarm_edit_set_time_btn_action(lv_obj_t * btn, lv_event_t e);
static void alarm_edit_set_repeat_btn_action(lv_obj_t * btn, lv_event_t e);
static void alarm_edit_del_btn_action(lv_obj_t * obj, lv_event_t e);

//common function
static void get_num_from_string(char * str, char * delimiters, hal_rtc_t * date_time);
static void get_optstr_from_num(uint8_t num, uint8_t num2, char * stropt, uint8_t type);

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void alarm_create_event_cb(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;

    if(LV_EVENT_CLICKED == e) {
        alarm_list_create(NULL);
    }
}

lv_obj_t * alarm_list_create(lv_obj_t * activity_obj)
{
    /* activity obj */
    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ALARM_LIST;
        activity_ext.create = alarm_list_create;
        activity_ext.prepare_destory = alarm_list_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    } else {
        lv_obj_clean(activity_obj);
    }

    /* watch obj */
    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    lv_alarm_list_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_alarm_list_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * title = lv_label_create(obj, NULL);
    lv_label_set_text_id(title, WATCH_TEXT_ID_ALARM);
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_obj_align(title, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * btn_add = lv_btn_create(obj, NULL);
    lv_obj_set_size(btn_add, LV_HOR_RES/5, LV_VER_RES/5);
    lv_obj_t * img_add = lv_img_create(btn_add, NULL);
    lv_img_set_src(img_add, ICON_PLUS);
    lv_obj_add_style(btn_add, LV_BTN_PART_MAIN, &lv_style_transp_tight);
    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(btn_add)) {
        lv_obj_align(btn_add, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    } else {
        lv_obj_align(btn_add, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
    }
    lv_obj_set_event_cb(btn_add, alarm_list_add_btn_action);

    lv_obj_t * list = lv_list_create(obj, NULL);
    lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES*4/5);
    lv_obj_align(list, title, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_page_set_edge_flash(list, false);
    lv_page_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);
    lv_watch_obj_add_element(list);
    lv_watch_obj_add_element(lv_page_get_scrl(list));

    lv_obj_add_style(list, LV_LIST_PART_BG, &lv_watch_style_transp);
    lv_obj_add_style(list, LV_LIST_PART_SCROLLBAR, &lv_watch_cont_opa1);
    lv_obj_set_style_local_bg_opa(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_pad_inner(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_left(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_right(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 5);
    ext->list = list;

    //show alarm list
    app_adaptor_alarm_t *alarm = alarm_get_alarm();

    for(uint8_t i = 0; i < NV_ALARM_MAX_ALARM_NUM; i++) {
        printf("%s: %d: %d, %d, %d, %d, %d\n",
                __FUNCTION__,
                i,
                alarm[i].valid,
                alarm[i].on_off,
                alarm[i].hour,
                alarm[i].min,
                alarm[i].repeat_bitmap);
    }

    for(uint8_t i = 0; i < NV_ALARM_MAX_ALARM_NUM; i++) {
        //get min index sorted by time
        int8_t min_index = alarm_list_get_min_index(alarm);
        if(min_index >= 0) {
            //add to list
            alarm_list_add(list, alarm, min_index);
            //clear min index
            Hal_Mem_Set(&alarm[min_index], 0, sizeof(app_adaptor_alarm_t));
        }
    }
    Hal_Mem_Free(alarm);

    return obj;
}

/**********************
*   STATIC FUNCTIONS
**********************/

static void alarm_list_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static lv_alarm_list_obj_ext_t * alarm_list_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALARM_LIST);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_alarm_list_obj_ext_t  * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

static void alarm_list_get_repeat_str(uint8_t repeat_bitmap, char *str, uint8_t len)
{
    if(NULL == str) {
        return;
    }

    memset(str, 0, len);
    if(0xFF == repeat_bitmap) {  // every day
        strncpy(str, lv_lang_get_text(WATCH_TEXT_ID_ALARM_EVERY_DAY), len);
    } else if(0x80 == (repeat_bitmap & 0x80)) {  // alarm repeat
        for(uint8_t i = 0; i < 7; i++) {
            if(0x01 == ((repeat_bitmap >> i) & 0x01)) {
                strcat(str, lv_lang_get_text(week_txt_id[i]));
                strcat(str, " ");
            }
        }
    } else {  //alarm once
        strncpy(str, lv_lang_get_text(WATCH_TEXT_ID_ALARM_NOT_REPEAT), len);
    }
}

static int8_t alarm_list_get_min_index(app_adaptor_alarm_t * alarm)
{
    int8_t min_index= -1;

    for(uint8_t i = 0; i < NV_ALARM_MAX_ALARM_NUM; i++) {
        if(1 == alarm[i].valid) {
            min_index = i;

            for (uint8_t j = i + 1; j < NV_ALARM_MAX_ALARM_NUM; j++) {
                if ((1 == alarm[j].valid) && ((alarm[j].hour * 60 + alarm[j].min) < (alarm[min_index].hour * 60 + alarm[min_index].min))) {
                    min_index = j;
                }
            }
            break;
        }
    }

    return min_index;
}

static lv_obj_t * alarm_list_add(lv_obj_t * list, app_adaptor_alarm_t * alarm, uint8_t index)
{
    char str[64];
    static uint16_t btn_size = 0;
    btn_size ++;
    /*Create a list element with the image an the text*/
    lv_obj_t * btn;
    btn = lv_btn_create(list, NULL);

    /*Set the default styles*/
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_20);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN,  LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_left(btn,  LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_top(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_line_width(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_OPA_20);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN,  LV_STATE_PRESSED, 15);
    lv_obj_set_style_local_pad_left(btn,  LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);
    lv_obj_set_style_local_pad_right(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);
    lv_obj_set_style_local_pad_top(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);
    lv_obj_set_style_local_pad_bottom(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);
    lv_obj_set_style_local_pad_inner(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);
    lv_obj_set_style_local_line_width(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);

    lv_page_glue_obj(btn, true);
    lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);
    lv_obj_set_event_cb(btn, alarm_list_edit_btn_action);
    lv_watch_set_free_num(btn, index);
    lv_watch_obj_add_element(btn);

    /*Make the size adjustment*/
    lv_coord_t w = lv_obj_get_width(list);
    uint8_t pad_w = w - lv_obj_get_style_pad_left(list, LV_LIST_PART_SCROLLABLE);
    lv_obj_set_size(btn, pad_w, 56);

    lv_obj_t * cont = lv_cont_create(btn, NULL);
    lv_obj_set_width(cont, pad_w);
    lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(cont, false);
    lv_cont_set_fit2(cont, LV_FIT_PARENT, LV_FIT_PARENT);

    /*time*/
    lv_obj_t * label_time = lv_label_create(cont, NULL);
    lv_label_set_long_mode(label_time, LV_LABEL_LONG_SROLL);
    lv_obj_set_size(label_time, w * 2 / 5, LV_VER_RES / 8);
    lv_obj_add_style(label_time, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_set_click(label_time, false);
    memset(str, 0, 64);
    snprintf(str, 64, "%02d:%02d", alarm[index].hour, alarm[index].min);
    lv_label_set_text(label_time, str);

    /*repeat*/
    lv_obj_t * label_repeat = lv_label_create(cont, NULL);
    lv_obj_add_style(label_repeat, LV_LABEL_PART_MAIN,  &lv_watch_font14);
    lv_obj_set_click(label_repeat, false);
    alarm_list_get_repeat_str(alarm[index].repeat_bitmap, str, 64);
    lv_label_set_text(label_repeat, str);

    /*switch*/
    lv_obj_t * sw = lv_switch_create(cont, NULL);
    lv_obj_set_size(sw, LV_HOR_RES/4, LV_VER_RES/8);
    lv_obj_set_event_cb(sw, alarm_list_switch_action);
    lv_watch_set_free_num(sw, index);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_BG, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_INDIC, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));

    if(1 == alarm[index].on_off) {
        lv_switch_on(sw, LV_ANIM_OFF);
    }

    /*bidi dir*/
    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(cont)) {
        lv_obj_set_base_dir(label_time, LV_BIDI_DIR_LTR);
        lv_label_set_align(label_time, LV_LABEL_ALIGN_RIGHT);
        lv_obj_align(label_time, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 5);
        lv_obj_align(label_repeat, label_time, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 0);
        lv_obj_align(sw, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
    } else {
        lv_obj_align(label_time, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 5);
        lv_obj_align(label_repeat, label_time, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
        lv_obj_align(sw, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
    }

    return btn;
}

static lv_obj_t * alarm_list_add_exceed_max_create(lv_obj_t * par)
{
    if(par == NULL) par = lv_scr_act();

    lv_obj_t * obj = lv_obj_create(par, NULL);
    lv_obj_set_size(obj, lv_obj_get_width(par), lv_obj_get_height(par));
    lv_obj_add_style(obj, LV_OBJ_PART_MAIN, &lv_style_transp_tight);

    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_cont_opa1);
    lv_obj_set_size(content, LV_HOR_RES*4/5, LV_VER_RES/5);
    lv_obj_align(content, obj, LV_ALIGN_CENTER, 0, -12);
    lv_cont_set_layout(content, LV_LAYOUT_CENTER);
    lv_obj_set_click(content, false);

    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20_yellow);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text_id(label, WATCH_TEXT_ID_ALARM_EXCEED_MAX);
    lv_obj_set_click(label, false);

    /* del obj when anim end*/
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_ready_cb(&a, (lv_anim_ready_cb_t)alarm_list_add_exceed_max_destroy);
    lv_anim_set_time(&a, 1000);
    lv_anim_start(&a);

    return obj;
}

static void alarm_list_add_exceed_max_destroy(lv_anim_t * anim)
{
    lv_obj_del(anim->var);
}

static void alarm_list_switch_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_VALUE_CHANGED != e) {
        return;
    }

    bool sw_state = lv_switch_get_state(obj);
    BOOL on_off = sw_state ? 1: 0;
    uint8_t index = lv_watch_get_free_num(obj);

    printf("%s: index=%d, on_off=%d\n", __FUNCTION__, index, on_off);

    //set alarm
    app_adaptor_alarm_t *alarm = alarm_get_alarm();
    alarm[index].on_off = on_off;
    alarm_set_alarm_by_ui(alarm, NV_ALARM_MAX_ALARM_NUM);
    Hal_Mem_Free(alarm);
}

static void alarm_list_add_btn_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_alarm_list_obj_ext_t * ext = alarm_list_get_ext();
    if(lv_list_get_size(ext->list) >= 5) {
        printf("%s: 5 alarms at most\n", __FUNCTION__);
        alarm_list_add_exceed_max_create(lv_obj_get_parent(obj));
        return;
    }

    //init current alarm
    Hal_Mem_Set(&alarm_curr, 0, sizeof(alarm_curr_t));

    //create 'alarm add' UI
    alarm_add_create(NULL);
}

static void alarm_list_edit_btn_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    //set current alarm
    Hal_Mem_Set(&alarm_curr, 0, sizeof(alarm_curr_t));
    alarm_curr.index= lv_watch_get_free_num(obj);
    printf("%s: index=%d\n", __FUNCTION__, alarm_curr.index);

    app_adaptor_alarm_t *alarm = alarm_get_alarm();
    alarm_curr.hour = alarm[alarm_curr.index].hour;
    alarm_curr.min = alarm[alarm_curr.index].min;
    alarm_curr.repeat_bitmap= alarm[alarm_curr.index].repeat_bitmap;
    Hal_Mem_Free(alarm);

    //create 'alarm edit' UI
    alarm_edit_create(NULL);
}

static lv_obj_t * alarm_set_time_roller_create(lv_obj_t * par, hal_rtc_t * date_time)
{
    lv_obj_t * content;
    lv_obj_t * roller1_cont;
    lv_obj_t * roller2_cont;
    lv_obj_t * roller1;
    lv_obj_t * roller2;
    lv_obj_t * label1;

    lv_alarm_list_obj_ext_t * ext = alarm_list_get_ext();

    lv_obj_t * label = lv_label_create(par, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_TIME_SETTING);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_height(label, LV_VER_RES/6);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);

    content =  lv_cont_create(par, NULL);
    lv_obj_set_size(content, LV_HOR_RES*5/6, LV_VER_RES/2);
    lv_obj_set_pos(content, 0, lv_obj_get_height(label));
    lv_cont_set_layout(content, LV_LAYOUT_ROW_MID);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_style_transp);
    lv_obj_set_style_local_pad_top(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_bottom(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_left(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_right(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_inner(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_align(content, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
    lv_obj_set_base_dir(content, LV_BIDI_DIR_LTR);

    /*roller1 hour*/
    roller1_cont = lv_cont_create(content, NULL);
    lv_obj_set_size(roller1_cont, 75, LV_VER_RES / 2);
    lv_cont_set_layout(roller1_cont, LV_LAYOUT_OFF);
    lv_obj_add_style(roller1_cont, LV_CONT_PART_MAIN, &lv_watch_style_transp);

    roller1 = lv_roller_create(roller1_cont, NULL);
    ext->roller[0] = roller1;
    lv_obj_add_style(roller1, LV_ROLLER_PART_BG, &lv_watch_font20_gray);
    lv_obj_set_style_local_text_line_space(roller1, LV_ROLLER_PART_BG, LV_STATE_DEFAULT, 20);
    lv_obj_set_style_local_bg_opa(roller1, LV_ROLLER_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_add_style(roller1, LV_ROLLER_PART_SELECTED, &lv_watch_font20_gray);
    lv_obj_set_style_local_text_line_space(roller1, LV_ROLLER_PART_SELECTED, LV_STATE_DEFAULT, 20);
    lv_obj_set_style_local_bg_opa(roller1, LV_ROLLER_PART_SELECTED, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_roller_set_visible_row_count(roller1, 3);

    /*label1*/
    label1 = lv_label_create(content, NULL);
    lv_obj_set_style_local_text_font(label1, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_BIG);
    lv_obj_set_style_local_text_color(label1, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE);
    lv_label_set_text(label1, ":");

    /*roller2 minute*/
    roller2_cont = lv_cont_create(content, roller1_cont);
    roller2 = lv_roller_create(roller2_cont, NULL);
    ext->roller[1] = roller2;
    lv_obj_add_style(roller2, LV_ROLLER_PART_BG, &lv_watch_font20_gray);
    lv_obj_set_style_local_text_line_space(roller2, LV_ROLLER_PART_BG, LV_STATE_DEFAULT, 20);
    lv_obj_set_style_local_bg_opa(roller2, LV_ROLLER_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_add_style(roller2, LV_ROLLER_PART_SELECTED, &lv_watch_font20_gray);
    lv_obj_set_style_local_text_line_space(roller2, LV_ROLLER_PART_SELECTED, LV_STATE_DEFAULT, 20);
    lv_obj_set_style_local_bg_opa(roller2, LV_ROLLER_PART_SELECTED, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_roller_set_visible_row_count(roller2, 3);

    lv_watch_set_free_num(roller1, 1);
    lv_watch_set_free_num(roller2, 2);

    char hour_opt[16];
    char min_opt[16];
    char sec_opt[16];
    memset(hour_opt, 0, 16);
    memset(min_opt, 0, 16);
    memset(sec_opt, 0, 16);

    get_optstr_from_num(date_time->tm_hour, 0, hour_opt, 2);
    get_optstr_from_num(date_time->tm_min, 0, min_opt, 3);
    get_optstr_from_num(date_time->tm_sec, 0, sec_opt, 3);

    lv_roller_set_options(roller1, hour_opt, LV_ROLLER_MODE_NORMAL);
    lv_obj_set_event_cb(roller1,  alarm_set_time_roller_action);

    lv_roller_set_options(roller2, min_opt, LV_ROLLER_MODE_NORMAL);
    lv_obj_set_event_cb(roller2, alarm_set_time_roller_action);

    char str[3] = "00";
    memset(ext->sel_str[0],0,6);
    memset(ext->sel_str[1],0,6);
    strncpy(ext->sel_str[0],str,2);
    strncpy(ext->sel_str[1],str,2);
    lv_roller_set_auto_fit(roller1, true);
    lv_roller_set_selected(roller1, 2, LV_ANIM_OFF);
    lv_watch_roller_set_sel_style(roller1, ext->sel_str[0]);

    lv_roller_set_auto_fit(roller2, true);
    lv_roller_set_selected(roller2, 2, LV_ANIM_OFF);
    lv_watch_roller_set_sel_style(roller2, ext->sel_str[1]);

    return content;
}

static void alarm_set_time_roller_action(lv_obj_t * roller, lv_event_t e)
{
    if(LV_EVENT_VALUE_CHANGED != e) {
        return;
    }

    char sel_str[4];
    char hour_opt[16];
    char min_opt[16];
    char sec_opt[16];
    uint8_t hour;
    uint8_t min;

    memset(sel_str, 0, 4);
    memset(hour_opt, 0, 16);
    memset(min_opt, 0, 16);
    memset(sec_opt, 0, 16);

    lv_roller_get_selected_str(roller, sel_str, 4);

    if(lv_obj_get_user_data(roller).user_num == 1) {
        hour = atoi(sel_str);
        get_optstr_from_num(hour, 0, hour_opt, 2);
        lv_roller_set_options(roller, hour_opt, LV_ROLLER_MODE_NORMAL);
        lv_roller_set_selected(roller, 2, false);

    } else if(lv_obj_get_user_data(roller).user_num == 2) {
        min = atoi(sel_str);
        get_optstr_from_num(min, 0, min_opt, 3);
        lv_roller_set_options(roller, min_opt, LV_ROLLER_MODE_NORMAL);
        lv_roller_set_selected(roller, 2, false);

    }
}

static lv_obj_t * alarm_opt_create(lv_obj_t * par)
{
    lv_obj_t * content = lv_cont_create(par, NULL);
    lv_cont_set_fit2(content, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(content, LV_HOR_RES);
    lv_cont_set_layout(content, LV_LAYOUT_ROW_MID);
    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_set_click(content, true);
    lv_obj_align(content, par, LV_ALIGN_IN_BOTTOM_MID, 0, -LV_VER_RES / 12);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_style_transp);
    lv_obj_set_style_local_pad_left(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 20);
    lv_obj_set_style_local_pad_right(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 20);
    lv_obj_set_style_local_pad_inner(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 70);

    lv_obj_t * btn;
    btn = lv_imgbtn_create(content, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_CANCEL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_CANCEL);
    lv_obj_set_event_cb(btn, alarm_cancel_btn_action);

    btn = lv_imgbtn_create(content, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_OK);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_OK);
    lv_obj_set_event_cb(btn, alarm_ok_btn_action);

    return content;
}

static void alarm_cancel_btn_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_obj_t * par = lv_obj_get_parent(lv_obj_get_parent(obj)); //watch obj
    uint32_t type = lv_watch_get_free_num(par);
    lv_obj_del(lv_obj_get_parent(par));

    if((ALARM_ADD_SET_TIME == type) || (ALARM_ADD_SET_REPEAT == type)) {
        lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALARM_ADD);
        if(activity_obj) {
            lv_obj_del(activity_obj);
        }
    }
}

static void alarm_ok_btn_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_obj_t * par = lv_obj_get_parent(lv_obj_get_parent(obj)); //watch obj
    uint32_t type = lv_watch_get_free_num(par);
    lv_alarm_list_obj_ext_t * ext = alarm_list_get_ext();

    if((ALARM_ADD_SET_TIME == type) || (ALARM_EDIT_SET_TIME == type)){
        //set time
        char sel_str1[4];
        char sel_str2[4];
        memset(sel_str1, 0, 4);
        memset(sel_str2, 0, 4);

        lv_roller_get_selected_str(ext->roller[0], sel_str1, 4);
        lv_roller_get_selected_str(ext->roller[1], sel_str2, 4);
        alarm_curr.hour = atoi(sel_str1);
        alarm_curr.min = atoi(sel_str2);
        printf("%s: type=%d, hour=%d, min=%d\n", __FUNCTION__, type, alarm_curr.hour, alarm_curr.min);

        if(ALARM_EDIT_SET_TIME == type) {
            //set alarm
            app_adaptor_alarm_t *alarm = alarm_get_alarm();
            alarm[alarm_curr.index].on_off = 1;
            alarm[alarm_curr.index].hour = alarm_curr.hour;
            alarm[alarm_curr.index].min = alarm_curr.min;
            alarm_set_alarm_by_ui(alarm, NV_ALARM_MAX_ALARM_NUM);
            Hal_Mem_Free(alarm);
        }
    } else if ((ALARM_ADD_SET_REPEAT == type) || (ALARM_EDIT_SET_REPEAT == type)){
        //set repeat bitmap
        alarm_curr.repeat_bitmap = 0;
        for(uint8_t i = 0; i < 7; i++) {
            if(lv_obj_get_state(ext->btn_week[i], LV_BTN_PART_MAIN) & LV_STATE_CHECKED) {
                alarm_curr.repeat_bitmap |= (1 << i);
            }
        }
        if(0 != alarm_curr.repeat_bitmap) {
            alarm_curr.repeat_bitmap |= 0x80;
        }
        printf("%s: type=%d, repeat_bitmap=%d\n", __FUNCTION__, type, alarm_curr.repeat_bitmap);

        //set alarm
        app_adaptor_alarm_t *alarm = alarm_get_alarm();

        if(ALARM_ADD_SET_REPEAT == type) {
            for(uint8_t i = 0; i < NV_ALARM_MAX_ALARM_NUM; i++) {
                if(0 == alarm[i].valid) { //get the first free index
                    alarm[i].valid = 1;
                    alarm[i].on_off = 1;
                    alarm[i].hour = alarm_curr.hour;
                    alarm[i].min = alarm_curr.min;
                    alarm[i].repeat_bitmap = alarm_curr.repeat_bitmap;
                    break;
                }
            }
        } else if(ALARM_EDIT_SET_REPEAT == type) {
            alarm[alarm_curr.index].on_off = 1;
            alarm[alarm_curr.index].repeat_bitmap = alarm_curr.repeat_bitmap;
        }

        alarm_set_alarm_by_ui(alarm, NV_ALARM_MAX_ALARM_NUM);
        Hal_Mem_Free(alarm);
    }

    if(ALARM_ADD_SET_TIME == type) {
        //del 'set time' UI
        lv_obj_del(lv_obj_get_parent(par));

        //create 'set repeat' UI
        lv_obj_t * watch_obj = lv_watch_obj_create(lv_watch_get_activity_obj(ACT_ID_ALARM_ADD));
        lv_watch_set_free_num(watch_obj, ALARM_ADD_SET_REPEAT);

        alarm_set_repeat_create(watch_obj);
        alarm_opt_create(watch_obj);
    } else {
        if (ALARM_ADD_SET_REPEAT == type) {
            //del 'alarm add' UI
            lv_obj_t *activity_obj = lv_watch_get_activity_obj(ACT_ID_ALARM_ADD);
            if(activity_obj) {
                lv_obj_del(activity_obj);
            }

            //recreate 'alarm list' UI
            alarm_list_create(lv_watch_get_activity_obj(ACT_ID_ALARM_LIST));
        } else {
            //recreate 'alarm edit' UI
            alarm_edit_create(lv_watch_get_activity_obj(ACT_ID_ALARM_EDIT));

            //recreate 'alarm list' UI
            alarm_list_create(lv_watch_get_activity_obj(ACT_ID_ALARM_LIST));
        }
    }
}

static lv_obj_t * alarm_set_repeat_create(lv_obj_t * par)
{
    lv_obj_t * label = lv_label_create(par, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_ALARM_REPEAT);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_height(label, LV_VER_RES/6);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * content =  lv_cont_create(par, NULL);
    lv_obj_set_size(content, LV_HOR_RES, LV_VER_RES/2);
    lv_obj_set_pos(content, 0, lv_obj_get_height(label));
    lv_cont_set_layout(content, LV_LAYOUT_GRID);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_style_transp);
    lv_obj_set_style_local_pad_top(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_bottom(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_left(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_right(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_obj_align(content, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_watch_obj_add_element(content);

    lv_alarm_list_obj_ext_t * ext = alarm_list_get_ext();

    for(uint8_t i = 0; i < 7; i++) {
        lv_obj_t * btn_week = lv_btn_create(content, NULL);
        lv_btn_set_checkable(btn_week, true);
        lv_obj_set_size(btn_week, 50, 50);
        lv_obj_set_style_local_bg_color(btn_week, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
        lv_watch_set_free_num(btn_week, i);
        lv_page_glue_obj(btn_week, true);
        lv_obj_add_protect(btn_week, LV_PROTECT_PRESS_LOST);
        ext->btn_week[i] = btn_week;

        lv_obj_t * label_week = lv_label_create(btn_week, NULL);
        lv_label_set_text_id(label_week, week_txt_id[i]);
        lv_obj_align(label_week, btn_week, LV_ALIGN_CENTER, 0, 0);
        lv_watch_obj_add_element(btn_week);

        if((0x80 == (alarm_curr.repeat_bitmap & 0x80)) && (0x01 == ((alarm_curr.repeat_bitmap >> i) & 0x01))) {
            lv_btn_toggle(btn_week);
        }
    }

    return content;
}

static lv_obj_t * alarm_add_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ALARM_ADD;
        activity_ext.create = alarm_add_create;
        activity_ext.prepare_destory = alarm_add_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    lv_watch_set_free_num(obj, ALARM_ADD_SET_TIME);

    //step 1: set time
    hal_rtc_t time;
    Hal_Rtc_Gettime(&time);

    alarm_set_time_roller_create(obj, &time);
    alarm_opt_create(obj);

    return obj;
}

static void alarm_add_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static lv_obj_t * alarm_edit_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ALARM_EDIT;
        activity_ext.create = alarm_edit_create;
        activity_ext.prepare_destory = alarm_edit_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    } else {
        lv_obj_clean(activity_obj);
    }

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);

    lv_obj_t * cont = lv_cont_create(obj, NULL);
    lv_cont_set_layout(cont, LV_LAYOUT_COLUMN_MID);
    lv_obj_set_size(cont, LV_HOR_RES, LV_VER_RES);
    lv_obj_add_style(cont, LV_CONT_PART_MAIN, &lv_style_transp);
    lv_obj_set_style_local_pad_top(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_bottom(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 20);
    lv_obj_set_y(cont, LV_VER_RES / 25);
    lv_obj_set_click(cont, false);
    lv_watch_obj_add_element(cont);

    lv_obj_t * title = lv_label_create(cont, NULL);
    lv_label_set_text_id(title, WATCH_TEXT_ID_ALARM_EDIT);
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_obj_align(title, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * btn_del = lv_btn_create(obj, NULL);
    lv_obj_set_size(btn_del, LV_HOR_RES/5, LV_VER_RES/5);
    lv_obj_t * img_del = lv_img_create(btn_del, NULL);
    lv_img_set_src(img_del, ICON_IMGVIEWER_DELETE_BLUE);
    lv_obj_add_style(btn_del, LV_BTN_PART_MAIN, &lv_style_transp_tight);
    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(btn_del)) {
        lv_obj_align(btn_del, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    } else {
        lv_obj_align(btn_del, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
    }
    lv_obj_set_event_cb(btn_del, alarm_edit_del_btn_action);

    /*time*/
    lv_obj_t * btn_time = lv_btn_create(cont, NULL);
    lv_obj_add_style(btn_time, LV_BTN_PART_MAIN, &lv_style_transp_fit);
    lv_btn_set_fit2(btn_time, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_btn_set_layout(btn_time, LV_LAYOUT_OFF);
    lv_obj_set_width(btn_time, LV_HOR_RES*3/4);
    lv_obj_set_event_cb(btn_time, alarm_edit_set_time_btn_action);
    lv_obj_add_protect(btn_time, LV_PROTECT_PRESS_LOST);
    lv_watch_obj_add_element(btn_time);

    lv_obj_t * cont_time = lv_cont_create(btn_time, NULL);
    lv_obj_add_style(cont_time, LV_OBJ_PART_MAIN, &lv_style_transp_fit);
    lv_obj_set_width(cont_time, lv_obj_get_width(btn_time));
    lv_obj_set_click(cont_time, false);
    lv_obj_align(cont_time, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_cont_set_layout(cont_time, LV_LAYOUT_ROW_MID);
    lv_cont_set_fit2(cont_time, LV_FIT_NONE, LV_FIT_TIGHT);

    lv_obj_t * img_time = lv_img_create(cont_time, NULL);
    lv_img_set_src(img_time, ICON_TIME);
    lv_obj_set_pos(img_time, 0, 0);

    lv_obj_t * label_settime = lv_label_create(cont_time, title);
    lv_label_set_text_id(label_settime, WATCH_TEXT_ID_TIME_SETTING);

    lv_obj_t * label_time = lv_label_create(btn_time, NULL);
    char time_str[ALARM_TIME_STR_LENGTH];
    memset(time_str, 0, ALARM_TIME_STR_LENGTH);
    snprintf(time_str, ALARM_TIME_STR_LENGTH, "%02d:%02d", alarm_curr.hour, alarm_curr.min);
    lv_label_set_text(label_time, time_str);
    lv_obj_add_style(label_time, LV_LABEL_PART_MAIN, &lv_watch_font20);
    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(cont_time)) {
        lv_obj_align(label_time, label_settime, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 10);
        lv_obj_set_base_dir(label_time, LV_BIDI_DIR_LTR);
    } else {
        lv_obj_align(label_time, label_settime, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    }

    /*repeat*/
    lv_obj_t * btn_repeat = lv_btn_create(cont, btn_time);
    lv_obj_set_event_cb(btn_repeat, alarm_edit_set_repeat_btn_action);
    lv_watch_obj_add_element(btn_repeat);

    lv_obj_t * cont_repeat = lv_cont_create(btn_repeat, cont_time);
    lv_obj_t * img_repeat = lv_img_create(cont_repeat, NULL);
    lv_img_set_src(img_repeat, ICON_DATE);
    lv_obj_set_pos(img_repeat, 0, 0);

    lv_obj_t * label_setrepeat = lv_label_create(cont_repeat, title);
    lv_label_set_text_id(label_setrepeat, WATCH_TEXT_ID_ALARM_REPEAT);

    lv_obj_t * label_repeat = lv_label_create(btn_repeat, label_time);
    lv_obj_add_style(label_repeat, LV_LABEL_PART_MAIN,  &lv_watch_font14);
    char str[64];
    alarm_list_get_repeat_str(alarm_curr.repeat_bitmap, str, 64);
    lv_label_set_text(label_repeat, str);
    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(cont_repeat)) {
        lv_obj_align(label_repeat, label_setrepeat, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 10);
    } else {
        lv_obj_align(label_repeat, label_setrepeat, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    }

    return obj;
}

static void alarm_edit_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void alarm_edit_set_time_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_alarm_list_obj_ext_t * ext = alarm_list_get_ext();
    if(NULL == ext) {
        printf("%s: ext is NULL\n", __FUNCTION__);
        //del 'alarm edit' UI
        lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALARM_EDIT);
        if(activity_obj) {
            lv_obj_del(activity_obj);
        }
        return;
    }

    lv_obj_t * label;
    hal_rtc_t date_time;
    char str[ALARM_TIME_STR_LENGTH];

    lv_obj_t * obj = lv_watch_obj_create(lv_watch_get_activity_obj(ACT_ID_ALARM_EDIT));
    lv_watch_set_free_num(obj, ALARM_EDIT_SET_TIME);

    memset(&date_time, 0, sizeof(hal_rtc_t));
    label = lv_obj_get_child(btn, NULL);
    memset(str, 0, ALARM_TIME_STR_LENGTH);
    strncpy(str, lv_label_get_text(label), ALARM_TIME_STR_LENGTH);

    get_num_from_string(str, ":", &date_time);

    alarm_set_time_roller_create(obj, &date_time);
    alarm_opt_create(obj);
}

static void alarm_edit_set_repeat_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_alarm_list_obj_ext_t * ext = alarm_list_get_ext();
    if(NULL == ext) {
        printf("%s: ext is NULL\n", __FUNCTION__);
        //del 'alarm edit' UI
        lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALARM_EDIT);
        if(activity_obj) {
            lv_obj_del(activity_obj);
        }
        return;
    }

    lv_obj_t * obj = lv_watch_obj_create(lv_watch_get_activity_obj(ACT_ID_ALARM_EDIT));
    lv_watch_set_free_num(obj, ALARM_EDIT_SET_REPEAT);

    alarm_set_repeat_create(obj);
    alarm_opt_create(obj);
}

static void alarm_edit_del_btn_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }
    printf("%s: index=%d\n", __FUNCTION__, alarm_curr.index);

    //set alarm
    app_adaptor_alarm_t *alarm = alarm_get_alarm();
    Hal_Mem_Set(&alarm[alarm_curr.index], 0, sizeof(app_adaptor_alarm_t));
    alarm_set_alarm_by_ui(alarm, NV_ALARM_MAX_ALARM_NUM);
    Hal_Mem_Free(alarm);

    //del 'alarm edit' UI
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALARM_EDIT);
    if(activity_obj) {
        lv_obj_del(activity_obj);
    }

    //recreate 'alarm list' UI
    alarm_list_create(lv_watch_get_activity_obj(ACT_ID_ALARM_LIST));
}

static void get_num_from_string(char * str, char * delimiters, hal_rtc_t * date_time)
{
    char * temp;
    uint8_t date = 0;
    if(strcmp(delimiters, "-") == 0) {
        date = 1;
    }

    temp = strtok(str, delimiters);
    if(temp) {
        if(date)
            date_time->tm_year = atoi(temp);
        else
            date_time->tm_hour = atoi(temp);
        temp = strtok(NULL, delimiters);
        if(temp) {
            if(date)
                date_time->tm_mon = atoi(temp);
            else
                date_time->tm_min = atoi(temp);
            temp = strtok(NULL, delimiters);
            if(temp) {
                if(date)
                    date_time->tm_mday = atoi(temp);
                else
                    date_time->tm_sec = atoi(temp);
            }
        }
    }
}

static void get_optstr_from_num(uint8_t num, uint8_t num2, char * stropt, uint8_t type)
{
    uint8_t opt_display[5];
    uint8_t i, pos = 0;
    uint8_t min = 1;
    uint8_t count = 0;
    if(type == 1) { //month
        count = 12;
    } else if(type == 2) { //hour
        min = 0;
        count = 24;
    } else if(type == 3) { //min&sec
        min = 0;
        count = 60;
    }

    opt_display[0] = ((num - min + count) - 2) % count + min;
    opt_display[1] = ((num - min + count) - 1) % count + min;
    opt_display[2] = num;
    opt_display[3] = (num + 1 - min) % count + min;
    opt_display[4] = (num + 2 - min) % count + min;


    for(i = 0; i < 5; i++) {
        stropt[pos++] = (opt_display[i] % 100) / 10 + 0x30;
        stropt[pos++] = opt_display[i] % 10 + 0x30;
        stropt[pos++] = 0x0A;
    }
    stropt [pos - 1] = 0;

}

#endif /*USE_LV_WATCH_ALARM_SET*/
